package com.powernode.controller;

import com.powernode.domain.Category;
import com.powernode.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@RequestMapping("prod/category")
@Api(tags = "分类管理接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("table")
    @ApiOperation("查询分类集合")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadCategoryList() {
        List<Category> categories = categoryService.list();
        return ResponseEntity.ok(categories);
    }


    @GetMapping("listCategory")
    @ApiOperation("查询父分类集合")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadCategoryParentList() {
        List<Category> categories = categoryService.selectCategoryParentList();
        return ResponseEntity.ok(categories);
    }


    @PostMapping
    @ApiOperation("新增分类")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public ResponseEntity<Void> addCategory(@RequestBody Category category) {
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }

    @GetMapping("category/categoryInfo")
    @ApiOperation("查询父分类集合")
    public ResponseEntity<List<Category>> loadFrontCategoryByPid(@RequestParam(required = false, defaultValue = "0") Long parentId) {
        List<Category> categories = categoryService.selectFrontCategoryByPid(parentId);
        return ResponseEntity.ok(categories);
    }

}
